package com.cch.qqsg.config;

import com.cch.qqsg.utils.ExcelAnotherData;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

@Component

public class ScheduleConfig extends ExcelAnotherData {
    @Autowired
    @Qualifier("data")
    private ExcelAnotherData data;

    public WebDriver driver;
    String baseUrl = "https://ui.ptlogin2.qq.com/cgi-bin/login?daid=8&appid=21000115&s_url=https%3A%2F%2Fsg.qq.com%2Fcomm-htdocs%2Fmilo_mobile%2Flogin.html%3Fs_url%3Dhttps%253A%252F%252Fsg.qq.com%252Fcp%252Fa20200228signin%252Findex.html%26sData%3D%26logo%3D%26ignoreQCUA%3D&hln_css=https://ossweb-img.qq.com/images/js/milo_mobile/biz/login/sy.png&style=8&low_login_enable=1&pt_ttype=1&hln_u_tips=%E8%AF%B7%E8%BE%93%E5%85%A5%E6%82%A8%E7%9A%84QQ%E5%8F%B7%E7%A0%81";            //自己测试的网址

    @Test
    /*@Scheduled(cron ="0 0 9 * * ?  ")*/
    @Scheduled(cron = "10 * * * * ?")
    public void testmethod() throws InterruptedException, IOException {
        // e:/driver/geckodriver.exe   webdriver.gecko.driver
        //  System.setProperty("webdriver.firefox.marionette", "/www/wwwroot/qqsg/geckodriver");
        File file = new File("/www/wwwroot/qqsg/geckodriver");
        FirefoxBinary firefoxBinary = new FirefoxBinary(file);
        driver = new FirefoxDriver((Capabilities) firefoxBinary);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(baseUrl);
        //excel存储数据
//            Object[][] testData = getTestData("G:\\test", "qqsg.xlsx", "Sheet1");
//            String word1= (String) testData[0][0];
//            String word2= (String) testData[0][1];
        String word1 = "********";
        String word2 = "*********";
        System.out.println("word1" + word1);
        System.out.println("word2" + word2);

        driver.findElement(By.id("u")).sendKeys(word1);
        driver.findElement(By.id("p")).sendKeys(word2);
        driver.findElement(By.id("go")).click();

        //等待3秒，等待搜索结果
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String ss = URLEncoder.encode(driver.getPageSource(), "utf-8");
        System.out.println(ss);

        try {
            Thread.sleep(5000);
            driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/a")).click();
            Thread.sleep(3000);

            driver.quit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
